/**
 * 获取系统信息 statusBarHeight 和 navBarHeight
 */
export const getSystemInfo = () => {
	const windowInfo = uni.getWindowInfo();
	const statusBarHeight = windowInfo.statusBarHeight;
	let navBarHeight = 0;
	// #ifdef MP-WEIXIN
	const appBaseInfo = uni.getAppBaseInfo();
	const menuButtonInfo = uni.getMenuButtonBoundingClientRect();
	navBarHeight =
		(menuButtonInfo.top - windowInfo.statusBarHeight) * 2 +
		menuButtonInfo.height;
	// #endif

	// #ifndef MP-WEIXIN
	navBarHeight = windowInfo.statusBarHeight + 44;
	// #endif
	return {
		statusBarHeight,
		navBarHeight,
	};
};

/**
 * 判断图片格式
 * @param {*} url
 * @returns
 */
export const judgeImageFormat = (url) => {
	const name = url.split('/').pop();
	const suffix = name.split('.').pop();
	if (['jpg', 'jpeg', 'png'].includes(suffix)) {
		return true;
	} else {
		uni.showModal({
			title: '温馨提示',
			content: '只支持jpg.jpeg,png格式图片',
			showCancel: false,
			success: function (res) {
				if (res.confirm) {
					return true;
				} else {
					return false;
				}
			},
		});
	}
};

/**
 * 检查权限
 * 没有权限则请求权限
 * @param {*} scope scope.camera  scope.album scope.address
 * @param {*} content 提示内容
 * @returns
 */
export const checkPermission = (scope, content = '需要您授权访问相册') => {
	return new Promise((resolve, reject) => {
		uni.getSetting({
			success: (res) => {
				if (!res.authSetting[scope]) {
					uni.authorize({
						scope,
						success: () => {
							resolve(true);
						},
						fail: () => {
							uni.showModal({
								title: '提示',
								content: content,
								confirmText: '去设置',
								success: (res) => {
									if (res.confirm) {
										uni.openSetting();
									}
								},
							});
							reject(false);
						},
					});
				} else {
					resolve(true);
				}
			},
		});
	});
};

/**
 * 图片压缩和尺寸缩小
 * @param {*} filePath
 * @returns
 */
export const processImage = async (filePath) => {
	return new Promise(async (resolve, reject) => {
		try {
			uni.showLoading({ title: '图片处理中...' });
			const { width: originWidth, height: originHeight } =
				await uni.getImageInfo({
					src: filePath,
				});
			console.log('压缩之前', originWidth, originHeight);

			// 计算缩放比例
			const maxLongSide = 1600;
			const longSide = Math.max(originWidth, originHeight);

			if (longSide > maxLongSide) {
				// 计算缩放比例
				const scale = maxLongSide / longSide;
				const targetWidth = Math.round(originWidth * scale);
				const targetHeight = Math.round(originHeight * scale);
				console.log('压缩中', targetWidth, targetHeight, scale);

				const ctx = uni.createCanvasContext('processCanvas');
				ctx.drawImage(filePath, 0, 0, targetWidth, targetHeight);
				ctx.draw(false, () => {
					wx.canvasToTempFilePath({
						canvasId: 'processCanvas',
						width: targetWidth,
						height: targetHeight,
						destWidth: targetWidth,
						destHeight: targetHeight,
						success: (res) => resolve(res.tempFilePath),
						fail: reject,
					});
				});
			} else {
				resolve(filePath); // 不需要压缩
			}
		} catch (e) {
			console.log(e);
			reject(e);
		} finally {
			uni.hideLoading();
		}
	});
};
